using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 挂载在世界地图上可拾取的物品身上，触发时往人物背包添加物品
/// </summary>
public class ItemPickUp : MonoBehaviour
{
    public ItemData_SO itemDataTemplate;
    public ItemData_SO itemData;
    private void Awake()
    {
        if (itemDataTemplate != null)
        {
            itemData = Instantiate(itemDataTemplate);
            // itemData = ScriptableObject.CreateInstance<ItemData_SO>();
        }
        if (itemData.itemType == ItemType.WEAPON)
        {
            // Debug.Log("随机改变武器属性");
            itemData.weaponData = Instantiate(itemDataTemplate.weaponData);
            // 拾取武器时随机改变属性
            itemData.weaponData.attackRange += (float)Math.Round(UnityEngine.Random.Range(-0.3f, 0.3f), 2);
            itemData.weaponData.coldDown += (float)Math.Round(UnityEngine.Random.Range(-0.2f, 0.2f), 2);
            itemData.weaponData.maxDamage += UnityEngine.Random.Range(0, 10);
            itemData.weaponData.minDamage += UnityEngine.Random.Range(-5, 0);
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            // 添加到背包
            InventoryManager.Instance.inventoryData.AddItem(itemData, itemData.itemAmount);

            // 刷新背包
            InventoryManager.Instance.inventoryUI.RefreshUI();
            // WORKFLOW: 自动装备物品
            // GameManager.Instance.playerStat.EquipWeapon(itemData);
            // 检查任务
            QuestManager.Instance.UpdateQuestProcess(itemData.itemName, itemData.itemAmount);
            // 移除物品
            Destroy(gameObject);
        }
    }
}
